<template>
  <div class="app-container">
    <el-form :model="formData"
             label-width="120px"
             label-position="left">
      <el-form-item label="大学">
        <el-select v-model.number="formData.universityId"
                   placeholder="请选择大学">
          <el-option v-for="item in universityList"
                     :key="item.universityId"
                     :label="item.universityName"
                     :value="item.universityId">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="年份">
        <el-select v-model="formData.year"
                   placeholder="请选择年份">
          <el-option v-for="item in yearList"
                     :key="item"
                     :label="item"
                     :value="item">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="批次">
        <el-select v-model="formData.batch"
                   placeholder="请选择批次">
          <el-option v-for="item in batchList"
                     :key="item.id"
                     :label="item.name"
                     :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="科目">
        <el-select v-model="formData.mType"
                   placeholder="请选择科目">
          <el-option key="-1"
                     label="全部"
                     value="-1"></el-option>
          <el-option v-for="item in mTypeList"
                     :key="item.id"
                     :label="item.name"
                     :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="招生代码">
        <el-input v-model="formData.universityNo"></el-input>
      </el-form-item>
      <el-form-item label="专业名称">
        <el-input v-model="formData.majorName"></el-input>
      </el-form-item>
      <el-form-item label="专业代码">
        <el-input v-model="formData.majorId"></el-input>
      </el-form-item>
      <el-form-item label="招生人数">
        <el-input v-model="formData.studentNum"></el-input>
      </el-form-item>
      <el-form-item label="学费/年">
        <el-input v-model="formData.tuition"></el-input>
      </el-form-item>
    </el-form>
    <div class="submit-box">
      <el-button type="primary"
                 :loading="loading"
                 style="width: 200px;"
                 @click.native.prevent="doSubmit">提交</el-button>
    </div>
  </div>
</template>

<script>
import { edit, getDetail } from '@/api/enrollment_plan'
import { getAll } from '@/api/university'
import { errorMsg, successMsg } from '@/utils/my'

export default {
  data () {
    return {
      loading: false,
      formData: {
        id: 0,
        universityId: '',
        year: '',
        batch: '',
        mType: '',
        universityNo: '',
        majorName: '',
        majorId: '',
        studentNum: '',
        tuition: ''
      },
      universityList: [],
      yearList: [],
      batchList: [
        { id: 1, name: '本科一批' },
        { id: 2, name: '本科二批' },
        { id: 3, name: '高职高专' }
      ],
      mTypeList: [
        { id: 1, name: '文科' },
        { id: 2, name: '理科' }
      ]
    }
  },

  created () {
    this.formData.id = this.$route.params.id
    this.genYearList()
    this.fetchData()
    this.fetchUniversityList()
  },

  methods: {

    fetchData () {
      let that = this
      getDetail({ id: that.formData.id }).then(res => {
        that.formData.universityId = res.data.enrollUniversityId
        that.formData.year = res.data.year
        that.formData.batch = res.data.batch
        that.formData.universityNo = res.data.enrollUniversityNo
        that.formData.mType = res.data.mType
        that.formData.majorName = res.data.majorName
        that.formData.majorId = res.data.majorId
        that.formData.studentNum = res.data.studentNum
        that.formData.tuition = res.data.tuition

      })
    },

    doSubmit () {
      let that = this, d = that.formData
      d.id = parseInt(d.id)
      d.universityId = d.universityId.length <= 0 ? 0 : parseInt(d.universityId)
      d.year = d.year.length <= 0 ? 0 : parseInt(d.year)
      d.batch = d.batch.length <= 0 ? 0 : parseInt(d.batch)
      d.universityNo = d.universityNo <= 0 ? 0 : parseInt(d.universityNo)

      d.mType = d.mType.length <= 0 ? 0 : parseInt(d.mType)
      d.majorName = d.majorName
      d.majorId = d.majorId
      d.studentNum = d.studentNum.length <= 0 ? 0 : parseInt(d.studentNum)
      d.tuition = d.tuition.length <= 0 ? 0 : parseInt(d.tuition)

      if (d.universityId.length <= 0) {
        return errorMsg('请选择大学')
      }
      if (d.year.length <= 0) {
        return errorMsg('请选择年份')
      }
      if (d.batch.length <= 0) {
        return errorMsg('请选择批次')
      }
      if (d.mType.length <= 0) {
        return errorMsg('请选择科目')
      }
      if (d.universityNo.length <= 0) {
        return errorMsg('请填写招生代码')
      }
      if (d.majorName < 0) {
        return errorMsg('请填写专业名字')
      }
      if (d.majorId.length < 0) {
        return errorMsg('请填写专业代码')
      }
      if (d.studentNum < 0) {
        return errorMsg('请填写招生人数')
      }
      if (d.tuition < 0) {
        return errorMsg('请填写每年学费')
      }
      that.loading = true
      edit(d).then(res => {
        that.loading = false
        if (res.code != 20000) {
          return errorMsg(res.message)
        }
        successMsg(res.message)
        setTimeout(function () {
          that.$router.push({ path: '/admin/enrollment-plan/list' })
        }, 1500)
      })
    },

    genYearList () {
      const that = this
      let d = new Date(),
        curYear = d.getFullYear()
      for (let i = 0; i < 4; i++) {
        that.yearList.push(curYear)
        curYear--
      }
    },

    fetchUniversityList () {
      const that = this
      getAll().then(res => {
        that.universityList = res.data
      })
    }

  }
}
</script>

<style>
.submit-box {
  height: 50px;
  line-height: 50px;
  text-align: center;
  padding-right: 20px;
}
</style>
